<template>
  <div class="main-container">
    <el-form :model="query" class="form-right" inline label-width="80px">
      <el-form-item>
        <el-input v-model="query.mobile" placeholder="根据手机号码搜索" prefix-icon="Search" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery()"> 查询</el-button>
        <el-button @click="resetQuery()">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="page.loading" :data="page.list">
      <el-table-column label="用户昵称" min-width="20" prop="nickname" />
      <el-table-column label="手机号码" min-width="20" prop="mobile" />
      <el-table-column label="考试时间" min-width="30" prop="examStartTime" />
      <el-table-column label="作答时长" min-width="20" prop="userAnswerTime">
        <template #default="scope">
          {{ formatTime(scope.row.userAnswerTime) }}
        </template>
      </el-table-column>
      <el-table-column label="考试结果" min-width="20">
        <template #default="scope">
          <enum-view :enum-name="'UserExamStatus'" :enum-value="scope.row.userExamStatus" />
        </template>
      </el-table-column>
      <el-table-column label="考试得分" min-width="20" prop="userScore" />
      <el-table-column label="阅卷方式" min-width="20">
        <template #default="scope">
          <enum-view :enum-name="'EvaluationMethod'" :enum-value="scope.row.evaluationMethod" />
        </template>
      </el-table-column>
      <el-table-column label="阅卷状态" min-width="20">
        <template #default="scope">
          <enum-view :enum-name="'UserEvaluationStatus'" :enum-value="scope.row.userEvaluationStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <template v-if="scope.row.examType === 1">
            <el-button link type="primary" @click="openUserRecord(scope.row)">考试记录</el-button>
          </template>
          <template v-if="scope.row.examType === 2">
            <el-button :disabled="scope.row.userExamStatus < 2" link type="primary" @click="handleExamEvaluation(scope.row)">批阅 </el-button>
            <!--
            <el-divider direction="vertical" />
            <el-button :disabled="scope.row.userExamStatus < 2" link type="primary" @click="openUserFace(scope.row)">人脸记录</el-button>
            -->
          </template>
        </template>
      </el-table-column>
    </el-table>
    <common-pagination v-model:page="page" @pagination="handlePage" />
  </div>
  <exam-user-record
    v-if="userRecordRef.visible"
    :exam-type="props.examType"
    :exam-id="props.examId"
    :user-id="userRecordRef.userId"
    :visible="userRecordRef.visible"
    @close="userRecordRef.visible = false"
  />
</template>
<script setup>
  import useTable from '@/utils/table'
  import { ref } from 'vue'
  import { examApi } from '@/api/exam'
  import ExamUserRecord from './ExamUserRecord.vue'
  import CommonPagination from '@/components/Common/Pagination/index.vue'
  import EnumView from '@/components/Enum/View/index.vue'
  import { formatTime } from '@/utils/base.js'
  import router from '@/router/index.js'

  const props = defineProps({
    examType: { type: Number, default: 0 },
    examId: { type: String, default: '' },
    periodId: { type: String, default: '' }
  })

  // 批阅
  const handleExamEvaluation = (item) => {
    router.push({ path: '/exam/record/evaluation', query: { recordId: item.examRecordId, examId: props.examId } })
  }

  // 查看用户考试记录
  const userRecordRef = ref({})
  const openUserRecord = (item) => {
    userRecordRef.value.userId = item.userId
    userRecordRef.value.visible = true
  }

  // 基础功能
  const { page, handlePage, query, handleQuery, resetQuery } = useTable(
    {
      page: examApi.examUserPage
    },
    { examType: props.examType, relateId: props.examType === 3 ? props.periodId : props.examId }
  )
</script>
